package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;

import server.SQLConnection;
/**
 * this class implements the get copy from db
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessagesGetCopy {
private ArrayList<String> copyList;

private ConnectionToClient client;

/**
 * constractor of this class 
 * @param client
 */
	public MessagesGetCopy(ConnectionToClient client){
		
		this.client=client;
		
	}
	
/**
 * getting this list of copy of books fromm the DB 
 * @return
 * @throws IOException
 */
	public ArrayList<String> GetCopy() throws IOException 
	{
		
		try {
			SQLConnection sc = new SQLConnection();
			PreparedStatement ps = sc.getConnection().prepareStatement("SELECT DISTINCT BID FROM bookscopies;");
			ResultSet rs = ps.executeQuery();
			
			copyList = new ArrayList<String>();
			copyList.add("Copylist");
			
	        while(rs.next())
	        {	
	        	copyList.add(rs.getString(1));
	       
	        }
	        
	   
	        
			rs.close();
		    client.sendToClient(copyList);
			return copyList;

		} catch (SQLException e) {
			
			e.printStackTrace();
			return null;
		}	
	}
}